import immutable from '../lib/immutable';
import { EVENT_TYPE_PRODUCT_ADD_CART, EVENT_TYPE_PRODUCT_BUY } from '../business/product/constrants'

const Product = {
    create: function create(config = {}) {
        return {
            handle: function handle(event, params = immutable.fromJS({})) {
                if (!event || !event.toJS || !params.toJS) return;
                switch (event.get('id')) {
                    case EVENT_TYPE_PRODUCT_ADD_CART: case EVENT_TYPE_PRODUCT_BUY: {
                        const listData = params.getIn(['listData']);
                        let fieldIdKey,
                            fieldIdValue = '';
                    // 找列表数据里面的对应id值
                        listData && listData.map((value, key) => {
                            const field = listData.getIn([key, 'field']);
                            if (field === 'id') {
                                fieldIdKey = key;
                                fieldIdValue = listData.getIn([key, 'value']);
                            }
                        });
                        const set_id = params.getIn(['set_id']);
                        config.onAddCartOrBuy && config.onAddCartOrBuy(event.get('id'), set_id, fieldIdKey, fieldIdValue, params);
                        break;
                    }
                    default: break;
                }
            },
        };
    },
};
export default Product;
